package com.wcl.demo.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.easemob.EMValueCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupInfo;
import com.easemob.chat.EMGroupManager;
import com.easemob.chat.GroupChangeListener;
import com.easemob.exceptions.EaseMobException;

import com.wcl.demo.R;
import com.wcl.demo.ui.activity.AddPublicGroupActivity;
import com.wcl.demo.ui.activity.ChatActivity;
import com.wcl.demo.ui.activity.GroupChatActivity;
import com.wcl.demo.ui.activity.MakeGroupActivity;
import com.wcl.demo.ui.adapter.GroupRecylcerViewAdapter;
import com.wcl.demo.ui.fragment.ItemInterface.GroupOnItemListener;
import com.wcl.demo.utils.ActivityUtils;

import java.util.List;

/**
 * Created by dllo on 15/11/19.
 */
public class GroupFragment extends Fragment implements View.OnClickListener,GroupOnItemListener {


    private RelativeLayout fragment_group_addItem, fragment_group_makeItem;
    private RecyclerView recyclerView;
    private GroupRecylcerViewAdapter adapter;
    public List<EMGroup> grouplist;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_group, null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        fragment_group_addItem = (RelativeLayout) getActivity().findViewById(R.id.fragment_group_addItem);
        fragment_group_makeItem = (RelativeLayout) getActivity().findViewById(R.id.fragment_group_makeItem);
        fragment_group_addItem.setOnClickListener(this);
        fragment_group_makeItem.setOnClickListener(this);
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.fragment_group_recyclerView);
        adapter = new GroupRecylcerViewAdapter();
        adapter.setGroupItemClickListener(this);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 1);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(manager);
        refresh();
        groupChatListener();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fragment_group_addItem:
                ActivityUtils.startToActivity(getActivity() , AddPublicGroupActivity.class);
                break;
            case R.id.fragment_group_makeItem:
                ActivityUtils.startToActivity(getActivity() , MakeGroupActivity.class);
                break;
        }
    }

    public void refresh() {

        grouplist = EMGroupManager.getInstance().getAllGroups();//需异步处理
        //     Log.i("jfdgjkl", grouplist + "");
        adapter.addData(grouplist);
    }

    @Override
    public void setOnGroupOnItemListener(EMGroup emGroup , int position) {
        Bundle bundle = new Bundle();
//        bundle.putString("groupId",grouplist.get(position).getGroupId());
//        ActivityUtils.startToActivity(getActivity() , GroupChatActivity.class);
        bundle.putString("username",emGroup.getGroupId());
        bundle.putString("title",emGroup.getGroupName());
        bundle.putBoolean("groupchat",true);
        ActivityUtils.startToActivity(getActivity(), ChatActivity.class, bundle);
    }
    /************************************************ 群聊的监听 ****************************************************/

    private void groupChatListener(){
        EMGroupManager.getInstance().addGroupChangeListener(new GroupChangeListener() {
            @Override
            public void onUserRemoved(String groupId, String groupName) {
                //当前用户被管理员移除出群聊
            }

            @Override
            public void onInvitationReceived(String groupId, String groupName, String inviter, String reason) {
                //收到加入群聊的邀请
                refresh();
            }

            @Override
            public void onInvitationDeclined(String groupId, String invitee, String reason) {
                //群聊邀请被拒绝
                refresh();
            }

            @Override
            public void onInvitationAccpted(String groupId, String inviter, String reason) {
                //群聊邀请被接受
                refresh();
            }

            @Override
            public void onGroupDestroy(String groupId, String groupName) {
                //群聊被创建者解散
                refresh();
            }

            @Override
            public void onApplicationReceived(String groupId, String groupName, String applyer, String reason) {
                //收到加群申请
                refresh();
            }

            @Override
            public void onApplicationAccept(String groupId, String groupName, String accepter) {
                //加群申请被同意
                refresh();
            }

            @Override
            public void onApplicationDeclined(String groupId, String groupName, String decliner, String reason) {
                // 加群申请被拒绝
                refresh();
            }
        });

    }


    /***************************************************************************************************************/

    @Override
    public void onResume() {
        super.onResume();
        refresh();
        adapter.notifyDataSetChanged();
    }

}
